package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.Classcardservice.ClassCardFieldService;
import com.wuseguang.springcloud.dao.jdbc.ClassRoomJdbc;
import com.wuseguang.springcloud.service.ClassRoomService;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class ClassRoomServiceImpl implements ClassRoomService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ClassRoomJdbc classRoomJdbc;

    @Autowired
    private ClassCardFieldService service;

    /**
     * @描述 按条件查询 教室相关信息
     * @创建人 zm
     * @创建时间 2020/1/9
     */
    @Override
    public Result queryClassRoomAll(int indexNo, String school_id, String classid, String classroomname, Integer rows) {
        logger.info("school_id={},classid={},classroomname={}", school_id, classid, classroomname);
        String csql = "";

        //按班级查询
        if (classid != null && !"".equals(classid) && !"0".equals(classid)) {
            csql += " AND c.id= " + classid;
        }

        //根据教室名称 模糊查询
        if (!StringUtils.isEmpty(classroomname)) {
            csql += " and m.name like '%" + classroomname + "%'";
        }

        csql += " ORDER BY CONVERT(classroomname USING gbk)";
        logger.info("csql={}", csql);
        String sql = " SELECT m.id classroomid,m.name classroomname,m.remarks,c.classname,c.id classid FROM  classroom m LEFT JOIN    (SELECT s.name" +
                " classname,s.id FROM grade g, classes s WHERE g.id=s.grade_id " +
                "                  AND g.school_id= s.school_id AND s.school_id=? ) c  ON   m.classid=c.id WHERE m.school_id=? " + csql;
        logger.info("sql={}", sql);
        List<Map<String, Object>> maps = null;
        try {
            maps = classRoomJdbc.queryListMap(sql, school_id, school_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(maps);
        //返回数据
        return new Result(new PaginationUtil<>(maps.size(), rows, tool.getList(), indexNo));
    }

    /**
     * @描述 删除教室  并删除和该教室绑定的设备
     * @创建人 zm
     * @创建时间 2020/1/9
     */
    @Transactional
    @Override
    public int delClassRoom(String school_id, int classroomid) {
        logger.info("school_id={},classroomid={}", school_id, classroomid);
        String sql = " DELETE FROM classroom WHERE school_id=? AND  id=?";
        String sql1 = "  DELETE FROM bancard WHERE school_id=? AND  classroomid=?";
        int i = classRoomJdbc.updateByParameter(sql, school_id, classroomid);
        if (i > 0) {
            classRoomJdbc.updateByParameter(sql1, school_id, classroomid);
        }
        return i;
    }

    /**
     * @描述 修改教室信息
     * @创建人 zm
     * @创建时间 2020/1/9
     */
    @Override
    public int updateClassRoom(String school_id, int classroomid, String classroomname, String classid, String remarks) {
        logger.info("school_id={},classid={},remarks={},classroomname={}", school_id, classid, remarks, classroomname);
        if (StringUtils.isEmpty(classid)) {
            classid = null;
        }
        String sql = "UPDATE classroom SET name=?,classid=? ,remarks=? WHERE school_id=? AND id=?";
        int i = classRoomJdbc.updateByParameter(sql, classroomname, classid, remarks, school_id, classroomid);
        if (i > 0) {
            service.classesAndCards(classid, "classflag", "1");
        }
        return i;
    }

    /**
     * @描述 教室绑定的班级置null
     * @创建人 zm
     * @创建时间 2020/2/23
     */
    @Override
    public int updateClassRoom(String school_id, int classid) {
        logger.info("school_id={},classid={}", school_id, classid);
        String sql = "UPDATE classroom SET classid=NULL WHERE school_id=? AND classid=?";
        int i = classRoomJdbc.updateByParameter(sql, school_id, classid);
        if (i > 0) {
            service.classesAndCards(classid, "classflag", "1");
        }
        return i;
    }

    /**
     * @描述 添加教室
     * @创建人 zm
     * @创建时间 2020/1/9
     */
    @Transactional
    @Override
    public long addClassRoom(String school_id, String classroomname, String classid, String remarks) {
        logger.info("school_id={},classid={},remarks={},classroomname={}", school_id, classid, remarks, classroomname);
        if (StringUtils.isEmpty(classid)) {
            classid = null;
        }
        String sql = "INSERT INTO classroom(name,classid,school_id,remarks) VALUES(?,?,?,?)";
        return classRoomJdbc.updateByParameterGetId(sql, classroomname, classid, school_id, remarks);
    }

    @Override
    public long querClassRoomCount(String school_id, String classroomname, Integer classroomid) {
        logger.info("school_id={},classroomname={}", school_id, classroomname);
        String sql = "  SELECT COUNT(1) count FROM classroom WHERE school_id=? AND NAME=? ";
        if (!StringUtils.isEmpty(classroomid + "") && classroomid != 0) {
            sql = " SELECT COUNT(1) count FROM classroom WHERE school_id=? AND id!=? AND NAME=? ";
            return classRoomJdbc.queryCount(sql, school_id, classroomid, classroomname);
        }
        return classRoomJdbc.queryCount(sql, school_id, classroomname);
    }

    @Override
    public long querClassRoomCountid(String school_id, String classid, Integer classroomid) {
        String sql = "  SELECT COUNT(1) count FROM classroom WHERE school_id=? AND classid=? ";
        if (!StringUtils.isEmpty(classroomid)) {
            sql = "SELECT COUNT(1) count FROM classroom WHERE school_id=? AND classid=? AND id!=? ";
            return classRoomJdbc.queryCount(sql, school_id, classid, classroomid);
        }
        return classRoomJdbc.queryCount(sql, school_id, classid);
    }
}
